package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.dto.AirportDto;
import xyz.zhouzhaodong.crm.entity.Airport;
import xyz.zhouzhaodong.crm.entity.Customer;
import xyz.zhouzhaodong.crm.mapper.AirportMapper;
import xyz.zhouzhaodong.crm.mapper.CustomerMapper;
import xyz.zhouzhaodong.crm.service.IAirportService;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 接机 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-06-19
 */
@Service
public class AirportServiceImpl extends ServiceImpl<AirportMapper, Airport> implements IAirportService {

    @Resource
    AirportMapper airportMapper;

    @Resource
    CustomerMapper customerMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage addAirport(AirportDto airportDto) {
        try {
            Airport airport = airportDtoToAirport(airportDto);
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            airport.setId(id);
            airport.setCreateUser(airportDto.getStaffId());
            airport.setCreateTime(new Date());
            airportMapper.insert(airport);
            return RestMessage.success(id);
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage deleteAirport(AirportDto airportDto) {
        try {
            Airport airport = new Airport();
            airport.setId(airportDto.getId());
            airport.setFlag(1);
            airport.setUpdateUser(airportDto.getStaffId());
            airport.setUpdateTime(new Date());
            airportMapper.updateById(airport);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage updateAirport(AirportDto airportDto) {
        try {
            Airport airport = airportDtoToAirport(airportDto);
            airport.setUpdateUser(airportDto.getStaffId());
            airport.setUpdateTime(new Date());
            airportMapper.updateById(airport);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    public RestMessage selectAirport(AirportDto airportDto) {
        QueryWrapper<Airport> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id", airportDto.getCustomerId());
        queryWrapper.eq("flag", airportDto.getFlag() == null ? 0 : airportDto.getFlag());
        List<Airport> list = airportMapper.selectList(queryWrapper);
        return RestMessage.success(list);
    }

    @Override
    public RestMessage selectAirportList(AirportDto airportDto) {
        Integer flag = airportDto.getFlag();
        if (flag == null) {
            airportDto.setFlag(0);
        }
        Page<AirportDto> page = new Page<>(airportDto.getPageNo(),
                airportDto.getPageSize() == 0 ? 10 : airportDto.getPageSize());
        IPage<AirportDto> list = airportMapper.selectAdmissionList(airportDto, page);
        for (AirportDto a : list.getRecords()) {
            Customer customer = customerMapper.selectById(a.getCustomerId());
            a.setCustomer(customer);
        }
        return RestMessage.success(list);
    }

    private Airport airportDtoToAirport(AirportDto airportDto) {
        Airport airport = new Airport();
        airport.setId(airportDto.getId());
        airport.setCustomerId(airportDto.getCustomerId());
        airport.setAirport(airportDto.getAirport());
        airport.setFlightNumber(airportDto.getFlightNumber());
        airport.setArrivalDate(airportDto.getArrivalDate());
        airport.setArrivalTime(airportDto.getArrivalTime());
        airport.setPickUp(airportDto.getPickUp());
        airport.setVip(airportDto.getVip());
        airport.setRemark(airportDto.getRemark());
        airport.setFlag(airportDto.getFlag());
        return airport;
    }

}
